#include "BaseGameEntity.h"
#include "Entity Manager.h"
#include "GameWorld.h"
#include "GLPrimitives.h"

int BaseGameEntity::m_iNextValidID = 0;


BaseGameEntity::BaseGameEntity(Vector2D pos, string name, int type):
						Entity(name),
						m_vPos(pos),
						m_ID(m_iNextValidID),
						m_EntityType(type)
{
	EntityMgr->RegisterEntity(this);
	m_iNextValidID++;
	isMoving = false;
	m_iDrawFlags = 0;

	// The drawing rect must be set so the building's position is at it's center
	Rect.x = pos.x - Rect.w/2;
	Rect.y = pos.y - Rect.h/2;

}

string BaseGameEntity::ID_String()
{
	return GetString(m_ID);
}

void BaseGameEntity::Update(double time_elapsed)
{
	if(World->SelectionBox() != InvalidRect)
	{
		if(this->Intersects(World->SelectionBox()))
		{
			Select();
		}
		else
		{
			Deselect();
		}
	}

	World->EnforceNonPenetration(this);
}

void BaseGameEntity::Render()
{
	applySurface();

	// Draw things according to the flags
	if(this->Selected())
		DrawRect();

	if(IsBoundCircleOn())
	{
		DrawPrimitives->DrawCircle(Pos().x - ClipPlane->GetX(), Pos().y - ClipPlane->GetY(), BRadius(), red);
	}
}

// ----------- Flags -----------------------
void BaseGameEntity::boundCircleOn() 
{
	m_iDrawFlags |= boundCircle;
}
void BaseGameEntity::collisionCircleOn() 
{
	m_iDrawFlags |= collisionCircle;
}
void BaseGameEntity::selectionRectOn() 
{
	m_iDrawFlags |= selectionRect;
}

void BaseGameEntity::boundCircleOff()
{
	if(On(boundCircle)) 
		m_iDrawFlags ^= boundCircle;
}
void BaseGameEntity::collisionCircleOff() 
{
	if(On(collisionCircle)) 
		m_iDrawFlags ^= collisionCircle;
}
void BaseGameEntity::selectionRectOff() 
{
	if(On(selectionRect)) 
		m_iDrawFlags ^= collisionCircle;
}

// Checks to see if a flag has been switch
bool BaseGameEntity::On(flag bt) {return (m_iDrawFlags & bt) == bt;}
bool BaseGameEntity::IsBoundCircleOn() {return On(boundCircle);}
bool BaseGameEntity::IsCollisionCircleOn() { return On(collisionCircle);}
bool BaseGameEntity::IsSelectionRectOn() {return On(selectionRect);}